<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class admin_model extends CI_Model {


	function __construct()
	{
		parent::__construct();
		//$this->load->database();
	}


	public function login($username,$password)
	{
		$data = array();
		$password = md5($password);
		$sql = 'select * from user where username="'.$username.'" and password = "'.$password.'"';
		$query = $this->db->query($sql);
	    $data = $query->row_array();
	    if($data)
	    {
			return $data;
	    }
	}

	//获取数据的行数，$table为对应的表，$day为对应的时间内，单位是天
	public function get_count($table,$day)
	{
		if($table && $day)
		{
			$difftime = $day*24*60*60;
			$lesstime = time()- $difftime;
			$sql = 'SELECT count(*) FROM '.$table.' WHERE Inputtime >='.$lesstime;
			$query = $this->db->query($sql);
			$data = $query->row_array();
			if($data)
			{
				return $data['count(*)'];
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}

	}

	//获取表信息
	public function get($table)
	{
		$sql = "SELECT * FROM '.$table.' WHERE 1";
		$query = $this->db->query($sql);
		return $query->row_array();
	}

	//获取表中所有信息
	public function getall($table)
	{
		$sql = 'SELECT * FROM '.$table.' WHERE 1';
		$query = $this->db->query($sql);
		return $query->result_array();
	}


	//插入数据,其中$table为对应的表，$data为要插入的数据
	public function add($table,$data='')
	{
		if($data && is_array($data) )
		{
			$this->db->insert($table,$data);

			return $this->db->affected_rows();
		}
		else
		{
			return false;
		}
	}

	//更新数据,其中$table为对应的表，$data为要更新的数据，$where为要更新的条件
	public function update($table,$data='',$where='')
	{
		if($data && $where && is_array($data) && is_array($where))
		{
			$this->db->update($table,$data,$where);
			return $this->db->affected_rows();
		}
		else
		{
			return false;
		}
	}

	//删除数据,其中$table为对应的表，$where 为要删除的条件
	public function delete($table,$where='')
	{
		if( $where  && is_array($where))
		{
			$this->db->delete($table,$where);
			return $this->db->affected_rows();
		}
		else
		{
			return false;
		}
	}

	//获取子分类
	public function get_category($pid=0,$type='new')
	{
		$sql = 'SELECT * from category WHERE ParentID='.$pid.' AND Type = "'.$type.'" ORDER BY ParentID ASC';
		$query = $this->db->query($sql);
		return $query->result_array();
	}

	//获取父分类
	public function get_pcategory($id,$type='new')
	{
		$returns = '';
		$sql = 'SELECT ParentID from category WHERE CategoryID='.$id.' AND Type = "'.$type.'" ORDER BY CategoryID ASC';
		$query = $this->db->query($sql);
		$result = $query->row_array();
		if($result['ParentID']!=0)
		{
			$returns .= $result['ParentID'].',';
			$returns .= $this->get_pcategory($result['ParentID']);
		}
		else
		{
			return $result['ParentID'];
		}
		return $returns;
	}

	//获取对应分类信息
	public function get_category_info($id)
	{
		$sql = 'SELECT * from category WHERE CategoryID='.$id.'  ORDER BY CategoryID ASC';
		$query = $this->db->query($sql);
		return $query->row_array();
	}

	//获取对应文章信息
	public function getnews($id)
	{
		$sql = 'SELECT * from news WHERE NewsId='.$id.'  ORDER BY NewsId ASC';
		$query = $this->db->query($sql);
		return $query->row_array();
	}

	//检测字段
	public function check_data($table,$data)
	{
		$sql = 'SELECT * FROM '.$table.' WHERE 1';
		$query = $this->db->query($sql);
		$result = $query->row_array();
		if(count($result)==0) return 2;
		$keys = array_keys($result);
		foreach($data as $k => $v)
		{
			$flag = 0;
			foreach($keys as $ks)
			{
				if($ks==$k)
				{
					$flag = 1;
					break;
				}
			}
			if(!$flag)
			{
				break;
			}
		}
		return $flag;
	}

}

/* End of file admin_model.php */
/* Location: ./application/models/admin_model.php */